package com.jn.erp.wechat.common.handler.cp;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.external.WxCpContactWayInfo;
import me.chanjar.weixin.cp.bean.external.WxCpContactWayResult;
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * <pre>
 *  企业微信客户相关配置
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/10/11 16:02
 */
@Component
public class CpExternalHandler {

    private final WxCpService wxCpService;

    @Autowired
    public CpExternalHandler(@Qualifier("wxCpSyncService") WxCpService wxCpService){
        this.wxCpService = wxCpService;
    }





    public void contactMsg() {
//        try {
////            wxCpService.getChatService().sendMsg();
////            wxCpService.getGroupRobotService().sendText();
//        }catch (WxErrorException e ){
//            System.out.println(e.getMessage());
//        }
    }


    /**
     *
     * https://wework.qpic.cn/wwpic3az/273422_w12SQ4LIQ_6FB2E_1728954724/0
     * [wmHBaWIwAAQVp6Ffs0PC43uk9-59xrsw]
     * WxCpExternalContactInfo(externalContact=me.chanjar.weixin.cp.bean.external.contact.ExternalContact@b46bb, followedUsers=[FollowedUser(userId=sunjiyong, remark=万幸得以相识💗（测试）, description=, createTime=1728955034, state=null, remarkCompany=null, remarkMobiles=[], tagIds=null, tags=[], remarkCorpName=null, addWay=1, operatorUserId=sunjiyong)])
     * */
    //获取外部学员列表
    public void contactList(String userId) {
        //sunjiyong
        try {
            List<String> list = wxCpService.getExternalContactService().listExternalContacts(userId);
            System.out.println(list);
            if(list.size()>0){
                for (int i=0;i<list.size();i++){
                    WxCpExternalContactInfo contactDetail = wxCpService.getExternalContactService().getContactDetail(list.get(i));
                    System.out.println(contactDetail);
                }
            }
        }catch (WxErrorException e ){
            System.out.println(e.getMessage());
        }
    }



    //获取已配置的「联系我」方式(二维码)
    public void getQrcodeContactWay(String configId) {
        //sunjiyong  0f2d60867d0f85b65af489d57a869339
        try {
            WxCpContactWayInfo contactWay = wxCpService.getExternalContactService().getContactWay(configId);
            if(contactWay!=null){
                String qrCode = contactWay.getContactWay().getQrCode();
                System.out.println(qrCode);
            }
        }catch (WxErrorException e){
            System.out.println(e.getMessage());
        }
    }


    //配置的「联系我」二维码
    public void addContactWay() {
        try {
            WxCpContactWayInfo info=new WxCpContactWayInfo();
            WxCpContactWayInfo.ContactWay contactWay=new WxCpContactWayInfo.ContactWay();
            contactWay.setType(WxCpContactWayInfo.TYPE.SINGLE);//联系方式类型,1-单人, 2-多人
            contactWay.setScene(WxCpContactWayInfo.SCENE.QRCODE);//场景，1-在小程序中联系，2-通过二维码联系
            contactWay.setUsers(Arrays.asList("sunjiyong"));//使用该联系方式的用户userID列表，在type为1时为必填，且只能有一个
            contactWay.setParties(Arrays.asList("5"));//使用该联系方式的部门id列表
            contactWay.setSkipVerify(true);//外部客户添加时是否无需验证
            contactWay.setRemark("api方式获取");
            info.setContactWay(contactWay);
            WxCpContactWayResult wxCpContactWayResult = wxCpService.getExternalContactService().addContactWay(info);
            System.out.println(wxCpContactWayResult.getQrCode());
            System.out.println(wxCpContactWayResult.getConfigId());
        }catch (WxErrorException e ){
            System.out.println(e.getMessage());
        }
    }


}
